package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavourMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavourMapper dishFlavourMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavourMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = new PageResult();
        int count = dishMapper.total(dishPageQueryDTO);
        dishPageQueryDTO.setPage((dishPageQueryDTO.getPage()-1)*dishPageQueryDTO.getPageSize());
        List<DishVO> records = dishMapper.pageQuery(dishPageQueryDTO);
        pageResult.setTotal(count);
        pageResult.setRecords(records);
        return pageResult;
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for(Long id: ids){
            Dish dish = dishMapper.getById(id);
            log.info("正在删除的菜品id为：{}", id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        List<Long>setmealIds=setmealDishMapper.getSetmealIdByDishId(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        for(Long id:ids){
            dishMapper.deleteById(id);
            dishFlavourMapper.deleteByDishId(id);
        }
    }
    @Override
    public List<Dish> dish(long categoryId) {
        List<Dish> dish = dishMapper.dish(categoryId);
        return dish;
    }

    @Override
    public void changestatus(Integer status, Long id) {
        dishMapper.changestatus(status,id);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = dishFlavourMapper.getByDishId(id);
        DishVO dishVO = DishVO.builder()
                .id(dish.getId())
                .name(dish.getName())
                .categoryId(dish.getCategoryId())
                .price(dish.getPrice())
                .image(dish.getImage())
                .description(dish.getDescription())
                .status(dish.getStatus())
                .updateTime(dish.getUpdateTime())
                .flavors(flavors)
                .build();
        return dishVO;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavourMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavourMapper.insertBatch(flavors);
        }

    }

    @Override
    public List<DishVO> getByIdWithFlavorLish(int categoryId) {
        List<DishVO> list = dishMapper.DishList(categoryId);
        for(DishVO dishVO:list){
            List<DishFlavor> flavors = dishFlavourMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(flavors);
        }
        return list;
    }

    @Override
    public boolean checkStatus(List<Long> ids) {
        for(Long id:ids){
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean getDish(Long id) {
        List<Dish> list = dishMapper.getDish(id);
        if(list != null && list.size() > 0){
            return true;
        }
        return false;
    }

}
